package com.dreamonerobot.molinkdemo;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.wifiview.config.HandlerParams;
import com.wifiview.images.SurfaceView;
import com.wifiview.nativelibs.StreamSelf;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private SurfaceView mSurfaceView;
    private StreamSelf mStreamSelf;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            Log.e(TAG, "handleMessage: " + msg.what);
            switch (msg.what) {
                case HandlerParams.SDCARD_FULL:
                    Toast.makeText(MainActivity.this, R.string.SD_card_full, Toast.LENGTH_LONG).show();
                    break;
                default:
                    break;
            }
            return true;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        mStreamSelf = new StreamSelf(getApplicationContext(), mSurfaceView, handler);
    }

    private void initView() {
        mSurfaceView = (SurfaceView) findViewById(R.id.mSurfaceView);
        ImageView ivMainTakePhoto = (ImageView) findViewById(R.id.iv_Main_TakePhoto);
        ivMainTakePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mStreamSelf.takePhoto();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mStreamSelf.startStream();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mStreamSelf.stopStream();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mStreamSelf.destroy();
    }
}
